#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "base64.h"

void base64_test_func() {
	unsigned char text[256] = { 0 };
	unsigned char encode[5000] = { 0 };
	unsigned char decode[5000] = { 0 };
	int enclen, declen;
	for (int i = 0; i < 256; i++) {
		text[i] = i;
	}
	enclen = base64_enclen((int)sizeof(text));
	printf("Encode evaluate size: %d\n", enclen);

	enclen = base64_encode(encode, text, (int)sizeof(text));
	printf("Encode real size: %d\n", enclen);

	declen = base64_declen(enclen);
	printf("Decode evaluate size: %d\n", declen);

	declen = base64_decode(decode, encode, enclen);
	printf("Decode real size: %d\n", declen);

	printf("Encode: %s\n", encode);

	char *test = "THlSYYgAAABfUwMAAArXIzwK1yM898ySOW8SgzkAAAA+AQABAAEAAQABAAIAAgACAAIAAgADAAMAAwADAAMABAAEAAQABAAEAAUABQAFAAUABQAGAAYABgAGAAYABwAHAAcABwAHAAgACAAIAAgACAAJAAkACQAJAAkACgAKAAoACgAKACJc5Q==";
	declen = base64_decode(decode, test, strlen(test));
	printf("Decode:%d\n", declen);
	for (int i = 0; i < declen; i++) {
		printf("%x ", decode[i]);
	}
	printf("\n");

	//FILE *fp = fopen("C:/Users/xt/Desktop/base64_testdata/[src]ti_send_lyra_3d.dat", "rb");
	//FILE *fp_w = fopen("C:/Users/xt/Desktop/base64_testdata/[dst]ti_send_lyra_3d.dat", "wb+");
	//long fLength;
	//unsigned char *buffer;
	//unsigned int offset = 0;

	//fseek(fp, 0, SEEK_END);
	//fLength = ftell(fp);
	//rewind(fp);

	////when load, malloc, may cos uart_send, mencopy failed.
	//buffer = (unsigned char *)malloc(fLength * sizeof(unsigned char));
	//fread(buffer, fLength, 1, fp);
	//fclose(fp);

	//enclen = base64_enclen(fLength);
	//printf("[P]Encode evaluate size: %d\n", enclen);

	//unsigned char *enc_buffer = (unsigned char *)malloc(enclen);
	//enclen = base64_encode(enc_buffer, buffer, fLength);
	//printf("[P]Encode real size: %d\n", enclen);

	//declen = base64_declen(enclen);
	//printf("[P]Decode evaluate size: %d\n", declen);
	//unsigned char *dec_buffer = (unsigned char *)malloc(declen);
	//declen = base64_decode(dec_buffer, enc_buffer, enclen);
	//printf("[P]Decode real size: %d\n", declen);
	//fwrite(dec_buffer, declen, 1, fp_w);
	//fclose(fp_w);
	//free(buffer);
	//free(enc_buffer);
	//free(dec_buffer);
}
